package com.personnalManagerSystem.controller;


import com.personnalManagerSystem.model.CompanyIncome;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-04-09
 */
@Controller
@RequestMapping("/company-income")  //公司收入
public class CompanyIncomeController  extends  BaseController{
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iCompanyIncomeService);
        return "view/companyIncome";
    }
    @RequestMapping("/add")
    public String add( @RequestParam(value = "incomeMoney",required = false)String incomeMoney,  //收入金额
                       @RequestParam(value = "expenditureMoney",required = false)String expenditureMoney, //支出金额
                       @RequestParam(value = "incomeDate",required = false)String incomeDate){//进账日期
        //新建一个空对象
        CompanyIncome companyIncome = new CompanyIncome();
        companyIncome.setIncomeMoney(new BigDecimal(incomeMoney));  //收入
        companyIncome.setExpenditureMoney(new BigDecimal(expenditureMoney)); //支出
        companyIncome.setIncomeDate(LocalDate.parse(incomeDate));  //记账时间
        iCompanyIncomeService.save(companyIncome);
        //通过获取最新的id
        List<CompanyIncome> companyIncomeList = iCompanyIncomeService.list();
        List<Long> ids = companyIncomeList.stream().map(CompanyIncome::getCompanyIncomeId).collect(Collectors.toList());
        Long id = ids.get(ids.size() - 1);  //获取了新添加的id
        Boolean b = iCompanyIncomeService.getCompanyProfit(id);
        return "redirect:/company-income/list";
    }


    @RequestMapping("/edit")
    public String edit(@RequestParam(value = "companyIncomeId",required = true)Long companyIncomeId,
                       @RequestParam(value = "incomeMoney",required = false)String incomeMoney,  //收入金额
                       @RequestParam(value = "expenditureMoney",required = false)String expenditureMoney, //支出金额
                       @RequestParam(value = "incomeDate",required = false)String incomeDate){//进账日期
        //新建一个空对象
        CompanyIncome companyIncome = new CompanyIncome();
        companyIncome.setCompanyIncomeId(companyIncomeId);
        companyIncome.setIncomeMoney(new BigDecimal(incomeMoney));  //收入
        companyIncome.setExpenditureMoney(new BigDecimal(expenditureMoney)); //支出
        companyIncome.setIncomeDate(LocalDate.parse(incomeDate));  //记账时间
        iCompanyIncomeService.saveOrUpdate(companyIncome);
        Boolean b = iCompanyIncomeService.getCompanyProfit(companyIncomeId);
        return "redirect:/company-income/list";
    }

    @RequestMapping("/del")
    public String del(@RequestParam(value = "companyIncomeId",required = false)Long companyIncomeId){
        iCompanyIncomeService.removeById(companyIncomeId);
        return "redirect:/company-income/list";
    }






}

